<template>
  <el-dialog
    :width="changeConfig.width"
    :title="changeConfig.title"
    :visible.sync="changeConfig.dialogFormVisible"
    :center="changeConfig.center"
    :close-on-click-modal="false"
    custom-class="changeDialog"
    @open="getParams"
    append-to-body
  >
    <PPMPage
      v-if="pageData.length > 0"
      :formData="formData"
      :pageData="pageData"
      @on-sumbit="saveProd"
      @goback="goback"
      :btnTextLF="'取消'"
      :btnTextRg="'确定'"
    >
    </PPMPage>
  </el-dialog>
</template>

<script>
import ppmmixin from "@/mixins/ppmmixin.js";
import modifySave from "@/utils/modifySave";
import OrderService from "@/api/order/OrderService.js";
import { mapState } from "vuex";
import { Loading } from "element-ui";
export default {
  name: "ChangePPM",
  mixins: [ppmmixin],
  props: {
    changeConfig: {
      type: Object,
      default: () => {
        return {};
      },
    },
    row: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      result: [],
      formLabelWidth: "120px",
      fullscreenLoading: false,
      ppmParams: {},
    };
  },
  computed: {
    ...mapState("orders", ["orderPosInfo"]),
    ...mapState("user", ["user", "busiManagerId", "custManagerId"]),
  },
  mounted() {},
  methods: {
    async getParams() {
      this.formData = {};
      this.pageData = {};
      this.ppmParams = JSON.parse(sessionStorage.getItem("ppmCondition"));
      sessionStorage.removeItem("proName");
      let result = await OrderService.getByProdId(this.ppmParams);
      if (result.code == 200) {
        //请求产品对应JS
        if (result.meta && result.meta !== {}) {
          let proName = result.meta.catalogName + result.meta.tempId;
          sessionStorage.setItem("proName", proName);
        }
      }

      let res = await this.qryPageTempAttr(this.ppmParams);
      if (res.code == 200) {
        //请求PPM配置
        this.pageData = res.meta.resList.pageModel;
        this.formData = res.meta.resList.paramModel;
        for (let key in this.formData) {
          if (key.split("-")[1] == "100012408") {
            this.formData[key].value = this.changeConfig.title;
          }
        }
      }
    },

    goback() {
      //取消
      this.changeConfig.dialogFormVisible = false;
    },

    async saveProd() {
      let arr = [];
      for (let key in this.formData) {
        arr.push(this.formData[key]);
      }
      this.result = [...arr];
      
      this.ppmParams = JSON.parse(sessionStorage.getItem("ppmCondition"));
      let params = modifySave.buildObject(
        this.result,
        this.row,
        this.ppmParams,
        this.orderPosInfo,
        this.user,
        this.changeConfig
      );
      
      let result = "";
      if (params) {
        let loadingInstance = Loading.service({ fullscreen: true });
        if (this.ppmParams.modType == "50200002") {
          //非正常变更
          result = await OrderService.saveAbnormalChange(params);
        }
        if (this.ppmParams.modType == "50200003") {
          //正常变更
          result = await OrderService.saveNormalChange(params);
        }
        
        if (result.code == 200) {
          if (result.meta.resultCode == 0) {
            this.changeConfig.dialogFormVisible = false;
            loadingInstance.close();
            this.$message({
              type: "success",
              message: "变更保存成功!",
            });

            this.customerOrder = result.meta.customerOrder;
            this.$store.commit("orders/SET_STEPACTIVE2", 2);
            this.$router.push({
              path: "/bgorderadd",
              query: {
                customerOrder: this.customerOrder,
              },
            });
            sessionStorage.setItem("bgCustomerOrder", JSON.stringify(this.customerOrder));
          } else {
            this.$message({
              type: "error",
              message: "保存失败!" + result.meta.resultMsg,
            });
            loadingInstance.close();
          }
        } else {
          this.$message.error("受理失败!请重新尝试");
          loadingInstance.close();
        }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.line {
  min-height: 0.62rem;
}
.ne-btn-title {
  display: inline-block;
  margin: 0 0 0.05rem 0.2rem;
  padding: 0.07rem 0.1rem;
  border-radius: 0.05rem;
  font-size: $fontSize_info;
  color: #fff;
  background-color: $orange;
}
.form-block {
  margin-top: 0.2rem;
}
::v-deep .changeDialog {
  min-height: 5rem;
  overflow-y: scroll;
  .el-dialog__header {
    text-align: left;
  }
  .el-dialog__title {
    border-left: 3px $blue solid;
    padding-left: 0.1rem;
  }
}
</style>
